package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleContentService;
import com.heima.article.service.ApArticleHttpService;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    protected ApArticleMapper apArticleMapper;
    /**
     * APP端文章列表显示
     * @param type 1更多 2.更新
     * @param dto 请求参数
     * @return
     */
    @Override
    public List<ApArticle> load(int type, ArticleHomeDto dto) {
        return apArticleMapper.loadArticleList(type,dto);
    }

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private ApArticleHttpService apArticleHttpService;
    /**
     * 创建和更新文章
     * @param dto
     * @return
     */
    @GlobalTransactional
    @Override
    public ResponseResult saveOrUpdateArticle(ArticleDto dto) {
        //1.dto导入到pojo
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        //2.根据文章id判断是创建还是更新
        if (apArticle.getId() == null){
            //创建APP文章主表数据
           this.save(apArticle);
            //创建APP文章配置表数据
            ApArticleConfig config = new ApArticleConfig();
            config.setIsComment(true);
            config.setIsForward(true);
            config.setIsDown(false);
            config.setIsDelete(false);
            config.setArticleId(apArticle.getId());
            apArticleConfigService.save(config);
            //创建APP文章内容表数据
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            apArticleContentService.save(content);
        } else{
            //3.更新APP文章主表数据
            int apArticleCount = this.count(Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getId, apArticle.getId()));
            if (apArticleCount == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"APP文章不存在");
            }
            this.updateById(apArticle);
            //更新APP文章内容表数据
            int apArticleContentCount = apArticleContentService.count(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, apArticle.getId()));
            if (apArticleContentCount == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"APP文章内容不存在");
            }
            apArticleContentService.update(Wrappers.<ApArticleContent>lambdaUpdate()
                    .eq(ApArticleContent::getArticleId,apArticle.getId())
                    .set(ApArticleContent::getContent,dto.getContent()));
        }

        //4.异步生成文章详情页
        apArticleHttpService.generatorHtml(apArticle, dto.getContent());

        //5.响应结果是文章主表id（方便自媒体表中获取主表id更新自媒体表）
        return ResponseResult.okResult(apArticle.getId());
    }
}
